package com.pengtu.web.app;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.dao.QueryFilter.MatchType;
import com.pengtu.entity.app.Attachment;
import com.pengtu.entity.app.WorkTask;
import com.pengtu.security.SpringSecurityUtils;
import com.pengtu.service.app.SystemManager;
import com.pengtu.service.app.WorkManager;
import com.pengtu.utils.DateUtils;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

@Result(name=BaseActionSupport.RELOAD,location="worktask.action",type="redirect")
public class WorktaskAction extends BaseActionSupport<WorkTask> {

	/**
	 * serialVersionUID:TODO
	 * 
	 * @since 1.0.0
	 */

	private static final long serialVersionUID = 1L;

	private WorkManager workManager;
	private SystemManager systemManager;
	private String id;
	private Attachment attachmention;
	private WorkTask workTask;
	private Page<WorkTask> page = new Page<WorkTask>(10);

	@Override
	public WorkTask getModel() {
		// TODO Auto-generated method stub
		return workTask;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		QueryFilter queryfilter = QueryFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		if(!page.isOrderBySetted()){
			page.order(Page.DESC);
			page.orderBy("createDate");
		}
		queryfilter.addFilter("publisher", SpringSecurityUtils.getCurrentUser().getUsername(), MatchType.EQ);
		page = workManager.findPageWorkTask(page,queryfilter);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		String usernames = Struts2Utils.getParameter("usernames");
		List<String> list =com.pengtu.utils.StringUtils.findString(usernames);
		workTask.setExecutor(list.toString().replace("[", "").replace("]", ""));
		if (null != attachmention) {
			attachmention.setTableId(workTask.getId());
			attachmention.setTableField("workTask_attachment");
			attachmention.setTableName("workTask");
			systemManager.uploadFile(Struts2Utils.getAppRootPath(),
					attachmention);
			workTask.setAttachment(attachmention);
		}
		if(StringUtils.isEmpty(id)){
			String  userName = SpringSecurityUtils.getCurrentUserName();
			workTask.setPublisher(userName);
		}
		workManager.saveWorkTask(workTask);
		addActionMessage("添加工作任务:"+workTask.getTitle());
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		workManager.deleteWorkTask(id);
		addActionMessage("删除工作任务!");
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if(StringUtils.isNotEmpty(id)){
			workTask = workManager.findPropertyWorkTask("id",id);
		}else{
			workTask = new WorkTask();
		}
	}

	@Autowired
	public void setWorkManager(WorkManager workManager) {
		this.workManager = workManager;
	}

	public Attachment getAttachmention() {
		return attachmention;
	}

	public void setAttachmention(Attachment attachmention) {
		this.attachmention = attachmention;
	}

	public Page<WorkTask> getPage() {
		return page;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Autowired
	public void setSystemManager(SystemManager systemManager) {
		this.systemManager = systemManager;
	}
	
	public String myExecuteTask() throws Exception {
		QueryFilter queryfilter = QueryFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		if(!page.isOrderBySetted()){
			page.order(Page.ASC);
			page.orderBy("createDate");
		}
		queryfilter.addFilter("executor", SpringSecurityUtils.getCurrentUserName(), MatchType.LIKE);
		page = workManager.findPageWorkTask(page,queryfilter);
		return "excute";
	}
	
	/**日程表显示*/
	public String view() throws Exception{
		
		return VIEW;
	}
	
	public String viewJson() throws Exception{
		
		/**获取我的全部任务*/
		String root = Struts2Utils.getRootPath();
		List<WorkTask> worklist = workManager.getWorktaskList(SpringSecurityUtils.getCurrentUser().getUsername());
		List<Map<String, Object>> mapWorkList = new ArrayList<Map<String, Object>>();
		for(WorkTask work: worklist){
			Map<String, Object> p = new HashMap<String, Object>();
			p.put("title", work.getTitle());
			p.put("start", DateUtils.convertDateToDayString(work.getPlanStarDate()));
			p.put("end", DateUtils.convertDateToDayString(work.getPlanEndDate()));
			p.put("id", work.getId());
			p.put("color", com.pengtu.utils.StringUtils.colorMapById(work.getId()));
			p.put("url",root+"/app/worktask!input.action?id="+work.getId());
			p.put("allDay", false);
			mapWorkList.add(p);
		}
		Struts2Utils.renderJson(mapWorkList);
		return NONE;
	}
	
	
	/**
	 * 定义在view()前执行二次绑定.
	 */
	public void prepareShow() throws Exception {
		prepareModel();
	}
	
	
	/**页面显示任务*/
	public String show() throws Exception{
		
		return "show";
	}
}
